package com.song.controller;

import com.song.po.NetClass;
import com.song.po.Room;
import com.song.po.Student;
import com.song.po.User;
import com.song.service.NetClassService;
import com.song.service.RoomService;
import com.song.service.StudentService;
import com.song.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class DormitoryController {
   @Autowired
   private UserService userService;
   @Autowired
   private NetClassService netClassService;
   @Autowired
   private StudentService studentService;
   @Autowired
   private RoomService roomService;

   /**
    * 登录
    * @param userName
    * @param password
    * @param session
    * @return
    */
   @RequestMapping(value = "loginUser",method = RequestMethod.POST)
   public String login(String userName, String password, HttpSession session){
      User user = userService.getUser(userName,password);
      if(user == null){
         return "loginErr";
      }else {
         session.setAttribute("loginUser",user);
         return "home";
      }
   }

   @RequestMapping(value = "back")
   public String back(HttpSession session){
      return "home";
   }


   @RequestMapping(value = {"/info/stu","/info/class","/room/msg"})
   public String jump(HttpServletRequest request){
      String uri = request.getRequestURI();
//      System.out.println(uri);
      if(uri .equals("/springDormitory/info/class")){
         List<NetClass> list = netClassService.getAllClassList();
         request.removeAttribute("classList");
         request.setAttribute("classList",list);
         return "netClass";
      }else if(uri .equals("/springDormitory/info/stu")){
         List<Student> list = studentService.getAllStudentList();
         request.setAttribute("studentList",list);
         return "student";
      }else{
         List<Room> list  = roomService.getAllRoomList();
         request.setAttribute("roomList",list);
         return "room";
      }
   }

   @RequestMapping(value = "queryClass",method = RequestMethod.POST)
   public String queryClass(NetClass netClass, HttpServletRequest request){
      List<NetClass> list = netClassService.getClassByWrapper(netClass);
      if(list != null){
         request.removeAttribute("classList");
         request.setAttribute("classList",list);
      }
      return "netClass";
   }
   @RequestMapping(value = "queryRoom",method = RequestMethod.POST)
   public String queryRoom(Room room, HttpServletRequest request){
      List<Room> list = roomService.getRoomByWrapper(room);
      if(list != null){
         request.removeAttribute("roomList");
         request.setAttribute("roomList",list);
      }
      return "room";
   }
   @RequestMapping(value = "queryStudent",method = RequestMethod.POST)
   public String queryStudent(Student student, HttpServletRequest request){
      List<Student> list = studentService.getStudentByWrapper(student);
      if(list != null){
         request.removeAttribute("studentList");
         request.setAttribute("studentList",list);
      }
      return "student";
   }
}
